//
//  HomeViewController.m
//  DIYCoupon
//
//  Created by 张睿 on 16/12/29.
//  Copyright © 2016年 张睿. All rights reserved.
//

#import "HomeViewController.h"
#import <Masonry.h>
#import "CollectionViewController.h"
#import "EditViewController.h"

@interface HomeViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

#define SCREEN6_SCALE ([UIScreen mainScreen].bounds.size.width /375)

@property (nonatomic, weak)UIButton * addImageButton;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    self.title = @"首页";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
}

#pragma mark - 添加子控件-
- (void)setupUI {
    //添加图片按钮
    UIButton * addButton = [[UIButton alloc] init];
    [addButton setImage:[UIImage imageNamed:@"img_add"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(pushToAlbum) forControlEvents:UIControlEventTouchUpInside];
    self.addImageButton = addButton;
    
    [self.view addSubview:self.addImageButton];
}

#pragma mark - 页面布局 -
- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    //添加图片按钮
    [self.addImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.view).offset(80);
    }];
    
}


- (void)pushToAlbum{
    
//    CollectionViewController * collectionVC = [CollectionViewController new];
//    
//    [self.navigationController pushViewController:collectionVC animated:YES];
    UIAlertController * CameraVC = [UIAlertController alertControllerWithTitle:@"照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }];
    
    [CameraVC addAction:cameraAction];
    
    UIAlertAction * albumAction = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }];
    
    [CameraVC addAction:albumAction];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [CameraVC addAction:cancelAction];
    
    [self presentViewController:CameraVC animated:YES completion:nil];
}

#pragma mark 图片选择器选择图片代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //关闭图片选择器
    [self dismissViewControllerAnimated:NO completion:nil];
    
    EditViewController * editVC = [EditViewController new];
    //取得选择图片
    UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
    
//    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//        UIImage * test = [self scaleImage:selectedImage toScale:0.18];
//        
//        editVC.editImage = test;
//        
//    }else{
//        UIImage * test = [self scaleImage:selectedImage toScale:0.15];
//        //        selectedImage.size = CGSizeMake(width / 10, height / 10);
//        
//        editVC.editImage=test;
//       
//    }
    editVC.editImage = selectedImage;
    
     [self.navigationController pushViewController:editVC animated:YES];
}

//- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{

//    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
//    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
//    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    
//    
//    return scaledImage;
//    CGFloat width = image.size.width;
//    CGFloat height = image.size.height;
//    
//    UIImage * scaleImage = image;
    
//}

@end
